package com.sino.operation.controller;

import com.sino.operation.param.inner.*;
import com.sino.operation.service.basic.ReleaseCacheService;
import com.sino.operation.service.business.TaskChoiceServiceImpl;
import com.sino.operation.commons.response.CenterResponseEntity;
import com.sino.operation.vo.TaskGetParam;
import com.sino.operation.vo.current.TaskOtherCompleteParam;
import com.sino.operation.vo.inner.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @ClassName TaskChoiceController
 * @Description 任务的暂挂、解卦、释放、回退、提交
 * @Author 北陈
 * @Date 2021/10/11 9:29
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("taskChoice")
@Api(value = "任务选择", tags = "暂挂、解卦、释放、回退、提交等操作")
public class TaskChoiceController {

    @Autowired
    private TaskChoiceServiceImpl taskChoiceService;

    @Autowired
    private ReleaseCacheService releaseCacheService;

    @PostMapping("getTask")
    @ApiOperation(value = "任务获取接口")
    public CenterResponseEntity<ResponseMessage> getTask(@RequestBody @Valid TaskGetParam taskGetParam) throws Exception {
        ResponseMessage message = taskChoiceService.getTask(taskGetParam);
        return CenterResponseEntity.build(ResponseMessage.class).setData(message);
    }

    /**
     * @Description: ${任务暂挂}
     * param []
     * return com.sino.operation.commons.response.CenterResponseEntity<java.lang.String>
     * author 北陈
     * date 2021/10/11 9:38
     */
    @PostMapping("suspend")
    @ApiOperation(value = "任务暂挂接口")
    public CenterResponseEntity<String> taskSuspend(@RequestBody @Valid TaskChoiceOtherParam taskChoiceOtherParam) {
        CenterResponseEntity<String> entity = CenterResponseEntity.build(String.class);
        try {
            taskChoiceService.taskSuspend(taskChoiceOtherParam);
        } catch (Exception e) {
            CenterResponseEntity<String> error = CenterResponseEntity.buildError(String.class);
            return error.setMsg("暂挂失败，" + e.getMessage());
        }
        return entity.setMsg("暂挂成功");
    }

    @PostMapping("resume")
    @ApiOperation(value = "任务解挂接口")
    public CenterResponseEntity<String> taskResume(@RequestBody Map<String,Object> param) {
        CenterResponseEntity<String> entity = CenterResponseEntity.build(String.class);
        try {
            String taskId = (String)param.get("taskId");
            taskChoiceService.taskResume(taskId);
        } catch (Exception e) {
            CenterResponseEntity<String> error = CenterResponseEntity.buildError(String.class);
            return error.setMsg("解挂失败，" + e.getMessage());
        }
        return entity.setMsg("解挂成功");
    }

    @PostMapping("release")
    @ApiOperation(value = "任务释放接口")
    public CenterResponseEntity<String> taskRelease(@RequestBody @Valid TaskChoiceOtherParam taskChoiceOtherParam) {
        CenterResponseEntity<String> entity = CenterResponseEntity.build(String.class);
        try {
            taskChoiceService.taskRelease(taskChoiceOtherParam);
        } catch (Exception e) {
            CenterResponseEntity<String> error = CenterResponseEntity.buildError(String.class);
            return error.setMsg("释放失败，" + e.getMessage());
        }
        return entity.setMsg("释放成功");
    }

    @PostMapping("back")
    @ApiOperation(value = "任务回退接口")
    public CenterResponseEntity<String> taskBack(@RequestBody @Valid TaskBackParam taskBackParam) throws Exception {
        CenterResponseEntity<String> entity = CenterResponseEntity.build(String.class);
        String result = taskChoiceService.taskBack(taskBackParam);
        entity.setData("回退成功");
        return entity;
    }

    @PostMapping("accountBack")
    @ApiOperation(value = "开变销回退接口")
    public CenterResponseEntity taskAccountBack(@RequestBody @Valid TaskBackByAccountParam taskBackByAccountParam) throws Exception {
        taskChoiceService.accountBack(taskBackByAccountParam);
        CenterResponseEntity<?> entity = CenterResponseEntity.build();
        return entity;
    }

    @PostMapping("accountComplete")
    @ApiOperation(value = "开变销(710307、710308、710309)的-外管、授权任务提交接口")
    public CenterResponseEntity taskAccountComplete(@RequestBody @Valid TaskCompleteByAccountParam taskCompleteByAccountParam) throws Exception {
        taskChoiceService.accountComplete(taskCompleteByAccountParam);
        CenterResponseEntity<?> entity = CenterResponseEntity.build();
        return entity;
    }

    @PostMapping("complete")
    @ApiOperation(value = "外管-授权任务提交接口")
    public CenterResponseEntity<String> taskComplete(@RequestBody @Valid TaskCompleteParam taskCompleteParam) throws Exception {
        String s = taskChoiceService.taskComplete(taskCompleteParam);
        CenterResponseEntity<String> entity = CenterResponseEntity.build(String.class);
        entity.setMsg("提交成功");
        return entity;
    }

    @PostMapping("CommonComplete")
    @ApiOperation(value = "公共提交接口")
    @ResponseBody
    public CenterResponseEntity<String> CommonComplete(@RequestBody @Valid TaskOtherCompleteParam taskCompleteParam) throws Exception {
        taskChoiceService.commonTaskComplete(taskCompleteParam);
        CenterResponseEntity<String> entity = CenterResponseEntity.build(String.class);
        return entity;
    }

    @PostMapping("taskTerminal")
    @ApiOperation(value = "任务终止接口")
    public CenterResponseEntity taskTerminal(@RequestBody @Valid TaskTerminalParam taskTerminalParam) throws Exception {
        taskChoiceService.taskTerminal(taskTerminalParam);
        String journalNo = taskTerminalParam.getJournalNo();
        releaseCacheService.release(journalNo);
        CenterResponseEntity<String> entity = CenterResponseEntity.build(String.class).setMsg("终止成功");
        return entity;
    }
}
